<?php

/**
 * This is the model class for table "packing_list_packages_cargo".
 */
class packing_list_packages_cargo extends ActiveRecord
{
	/**
	 * The followings are the available columns in table 'packing_list_packages_cargo':
	 * @var integer $id
	 * @var integer $package_id
	 * @var integer $cargo_id
	 * @var integer $detail_id
	 * @var integer $c_date
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return packing_list_packages_cargo the static model class
	 */
	public $cargo_ref;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'packing_list_packages_cargo';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('package_id, c_date, cargo_id', 'required'),
			array('package_id, cargo_id, c_date', 'numerical', 'integerOnly'=>true),
			array('package_id','exist','className'=>'packing_list_packages','attributeName'=>'id'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, package_id, cargo_id, c_date', 'safe', 'on'=>'search'),
		);
	}
	public function beforeValidate(){
		$this->c_date = time();
		return parent::beforeValidate();
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cargo'=>array(self::BELONGS_TO,'cargo','cargo_id'),
			'dimension'=>array(self::HAS_ONE,'packing_list_packages_cargo_dimension','foreign_key'),
			'package'=>array(self::BELONGS_TO,'packing_list_packages','package_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function afterFind(){
		if($this->dimension == null)
			$this->dimension = new packing_list_packages_cargo_dimension;
		return parent::afterFind();
	}
	public function delete(){
		if(parent::delete()) {
			if(($model = $this->dimension)!=null){
				$model->delete();
			}
			return true;		
		}
		else
			return false;
	}
	public function deleteByCargo($cargo_id){
		$model = parent::findAll('cargo_id=:cargo_id',array('cargo_id'=>$cargo_id));
		foreach($model as $_model)
			$_model->delete();
	}
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'package_id' => 'Packages',
			'cargo_id' => 'Cargo',
			'detail_id' => 'Detail',
			'c_date' => 'C Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('package_id',$this->package_id);

		$criteria->compare('cargo_id',$this->cargo_id);

		$criteria->compare('detail_id',$this->detail_id);

		$criteria->compare('c_date',$this->c_date);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}